package com.open.yujiakj.common.utils.bean;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * @program: maobao-parent
 * @description:
 * @author: honglei
 * @create: 2022-07-28 13:53
 */
public class BeanInitUtil {
    public static void initBean(Object object){
        //获得字节码对象
        Class a = object.getClass();
        Field[] fields = a.getDeclaredFields();

        for (Field f : fields)

        {
            String type = f.getGenericType().toString();
            String name = f.getName();

            f.setAccessible(true);

//如果有需要的字段就填充到obj中

            if (name.equals("id")) {
                try {
                    Object value=f.get(object);
                    if (Objects.nonNull(value)){
                        f.set(object, value.toString().toUpperCase());
                        continue;
                    }
                } catch (Exception e) {
                }
                try {

                    f.set(object, UUID.randomUUID().toString().toUpperCase());
                } catch (Exception e) {

                }
            }
            if (name.equals("createUserId")) {
                try {
                    f.set(object, "11111111-1111-1111-1111-111111111111");
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("creater")) {
                try {
                    f.set(object, "管理员");//填充值
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("createDt")) {
                try {
                    f.set(object,new Date());//填充值
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("updater")) {
                try {
                    f.set(object, "管理员");//填充值
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("updateUserId")) {
                try {
                    f.set(object, "11111111-1111-1111-1111-111111111111");
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("updateDt")) {
                try {
                    f.set(object, new Date());//填充值
                } catch (IllegalAccessException e) {

                }
            }
            if (name.equals("isDelete")) {
                try {
                    if (  type.contains("String")){
                        f.set(object, "0");//填充值
                    }
                    if (  type.contains("Integer")){
                        f.set(object, 0);//填充值
                    }
                    if (  type.contains("Double")){
                        f.set(object, 0d);//填充值
                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }
            }

        }
    }
}
